package com.topica.database.task;

import java.util.ArrayList;
import java.util.Random;

import android.content.Context;
import android.util.Log;

import com.topica.database.DataBaseHelper;
import com.topica.database.callback.OnGetQuizListener;
import com.topica.object.QuestionItem;
import com.topica.object.QuizItem;
import com.topica.util.Util;

public class GetQuizTask extends TopicaDBTask {
	private static final String TAG = "GetQuizTask";
	private String quizId;
	private String weekId;
	private QuizItem quizItem;
	private ArrayList<QuestionItem> questionItems;
	private int ERROR = 3;
	private int NODATA = 4;
	private int BEFORE_TIME = 5;
	private int AFTER_TIME = 6;

	private OnGetQuizListener listener;
	private long currentTime;

	public GetQuizTask(Context context, String quizId, String weekId) {
		super(context);
		this.quizId = quizId;
		this.weekId = weekId;
	}

	@Override
	protected void onPreExecute() {
	}

	@Override
	protected Integer doInBackground(Void... params) {

		DataBaseHelper myDbHelper = new DataBaseHelper(
				context.getApplicationContext());
		int time = myDbHelper.getPracticeTime(weekId);
		if (time == 0) {
			return ERROR;
		}
		ArrayList<Integer> ids = myDbHelper.getListQuiz(quizId);
		int n = ids.size();
		if (n == 0)
			return NODATA;
		Random random = new Random();
		int index = random.nextInt(n);
		int rowId = ids.get(index);
		quizItem = myDbHelper.getQuizItem("" + rowId, quizId);

		this.currentTime = System.currentTimeMillis();

		String openTime = quizItem.getTimeOpen();
		String closeTime = quizItem.getTimeClose();

		if (null != openTime && null != closeTime) {
			if (!openTime.equals("") && !closeTime.equals("")) {
				long lOpenTime = Util.convertDateToMinisecond(openTime);
				long lCloseTime = Util.convertDateToMinisecond(closeTime);

				if (currentTime < lOpenTime) {
					return BEFORE_TIME;
				} else if (currentTime > lCloseTime) {
					return AFTER_TIME;
				}
			}
		}
		publishProgress("Loading question..");
		String[] questionIds = quizItem.getQuestIds();
		questionItems = new ArrayList<QuestionItem>();
		for (String id : questionIds) {
			questionItems.add(myDbHelper.getListQuestion(id));
		}
		myDbHelper.close();
		return OK;
	}

	@Override
	protected void onPostExecute(Integer result) {
		// super.onPostExecute(result);
		if (result == OK) {
			listener.onGetQuizListener(quizItem, questionItems);
		} else if (result == NODATA) {
			listener.onNoData();
		} else if (result == AFTER_TIME) {
			listener.onAfterTimeTest();
		} else if (result == BEFORE_TIME) {
			listener.onBeforeTimeTest();
		} else if (result == ERROR) {
			listener.onTimeoutQuize(null);
		} else {
			Log.e(TAG, "Co loi trong qua trinh lay quiz");
		}
	}

	@Override
	protected void onProgressUpdate(String... values) {
		super.onProgressUpdate(values);
		dialog.setTitle(values[0]);
	}

	public void setOnGetQuizListener(OnGetQuizListener listener) {
		this.listener = listener;
	}

}
